package com.buybuyyi.stylistcrm.resource.controllers.goods;

import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.buybuyyi.stylistcrm.resource.goods.service.FileUploadService;

import stylistcrm.core.services.goods.dto.FileUploadDTO;
import stylistcrm.core.services.goods.service.IFileUploadService;
import stylistcrm.core.services.order.exception.BizException;
import stylistcrm.core.services.user.User;
import stylistcrm.core.services.user.service.UserService;

@Controller
@RequestMapping("/file")
public class FileUploadController {
   
	@Autowired
	IFileUploadService fileUploadService;
	
	@Autowired
	UserService userService;
	
	@RequestMapping("fileUpload")
	@ResponseBody
	public FileUploadDTO uploadFile(@RequestParam(value = "file", required = false) MultipartFile file, 
			            @RequestParam(value="type",required = false) String type,HttpServletRequest request) throws BizException {
		
		FileUploadDTO fileUploadDto=new FileUploadDTO();
		User user=userService.getCurrentUser();
		if(user == null){
			fileUploadDto.setIsSuccess("0");
			return fileUploadDto;
		}
		String fileName=file.getOriginalFilename();
		InputStream stream;
		try {
			stream = file.getInputStream();
			fileUploadDto=fileUploadService.fileUpload(stream, fileName, user.getMemberId(),type);
		} catch (Exception e) {
			fileUploadDto.setIsSuccess("0");
			fileUploadDto.setMessage("系统异常");
		}
		return fileUploadDto;
	}
}
